package com.pedro.streamer.view;

import android.content.Context;
import android.os.Environment;

import android.graphics.BitmapFactory;

import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.pedro.encoder.input.gl.render.filters.object.BaseObjectFilterRender;
import com.pedro.encoder.input.gl.render.filters.object.ImageObjectFilterRender;
import com.pedro.encoder.utils.gl.TranslateTo;
import com.pedro.streamer.R;
import com.pedro.streamer.adapter.StickerAdapter;
import com.pedro.streamer.manager.VideoArrManager;
import com.pedro.streamer.model.ImageObjectFilterModel;
import com.pedro.streamer.utils.FileUtils;
import com.pedro.streamer.utils.RecyclerViewSpacesItemDecoration;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

import com.pedro.streamer.R;
import com.pedro.streamer.adapter.StickerAdapter;
import com.pedro.streamer.manager.VideoArrManager;

import java.util.ArrayList;
import java.util.List;

public class CameraStickerControlView extends ConstraintLayout {
    private String TAG = "com.bmi.vcam";
    private RecyclerView stickerListView;
    private StickerAdapter stickerAdapter;

    private ConstraintLayout sticker_control_panel;
    private String picPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/TextureMap";
    private List<String> picArr;
    private VideoArrManager videoArrManager = VideoArrManager.shareInstance();
    private SeekBar position_leftright_seek_bar, position_updown_seek_bar, position_scale_seek_bar;
    private ImageView sticker_sure, sticker_cancel;

    private ImageView currentItemCheckImage;
    private BaseObjectFilterRender currentObjectFilterRender;
    private int currentStickerIndex;

    public CameraStickerControlView(@NonNull Context context) {
        super(context);
    }

    public CameraStickerControlView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

        initView();

    }

    public void reloadView() {

        if (stickerListView != null) {

            picArr = FileUtils.getAllImagePaths(picPath);
            if (picArr == null) {
                picArr = new ArrayList<>();
            }
            stickerAdapter.setStickerList(picArr);
        }
    }

    private void initView() {

        stickerListView = findViewById(R.id.sticker_list_view);
        sticker_control_panel = findViewById(R.id.sticker_control_panel);
        position_leftright_seek_bar = findViewById(R.id.position_leftright_seek_bar);
        position_updown_seek_bar = findViewById(R.id.position_updown_seek_bar);
        position_scale_seek_bar = findViewById(R.id.position_scale_seek_bar);
        picArr = FileUtils.getAllImagePaths(picPath);

        sticker_sure = findViewById(R.id.sticker_sure);
        sticker_cancel = findViewById(R.id.sticker_cancel);
        sticker_sure.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                sticker_control_panel.setVisibility(GONE);

            }
        });

        sticker_cancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                for (ImageObjectFilterModel imageObjectFilterModel : videoArrManager.getImageObjectFilterRenders()) {
                    if (imageObjectFilterModel.getImagePaths().equals(picArr.get(currentStickerIndex))) {
                        videoArrManager.getOpenGlView().removeFilter(imageObjectFilterModel.getImageObjectFilterRender());
                        videoArrManager.getImageObjectFilterRenders().remove(imageObjectFilterModel);
                        currentItemCheckImage.setSelected(!currentItemCheckImage.isSelected());
                        sticker_control_panel.setVisibility(GONE);
                        return;
                    }
                }
            }
        });

        if (picArr == null) {
            picArr = new ArrayList<>();
        }

        position_leftright_seek_bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

                Log.d(TAG, "onProgressChanged: " + i);
                float x = i / 100.00f;
                Log.d(TAG, "x: " + x);
                currentObjectFilterRender.setmCenterPointX(-x);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        position_updown_seek_bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                Log.d(TAG, "onProgressChanged: " + i);
                float y = i / 100.00f;
                Log.d(TAG, "y: " + y);
                currentObjectFilterRender.setmCenterPointY((float) (y-10.00));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        position_scale_seek_bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                currentObjectFilterRender.setScale(i*1.0f, i*1.0f);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });



        stickerAdapter = new StickerAdapter();

        HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
        stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.LEFT_DECORATION, 40);
        stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.BOTTOM_DECORATION, 40);
        stickerListView.addItemDecoration(new RecyclerViewSpacesItemDecoration(stringIntegerHashMap));

        stickerListView.setLayoutManager(new GridLayoutManager(getContext(), 4));
        stickerListView.setAdapter(stickerAdapter);
        stickerAdapter.setStickerList(picArr);
        stickerAdapter.setOnStickerItemClick(new StickerAdapter.OnStickerItemClick() {
            @Override
            public void onItemClick(Integer position) {


            }

            @Override
            public void onChoosePic(Integer position, boolean isSelect, ImageView checkImg) {
                if (isSelect) {

                    for (ImageObjectFilterModel imageObjectFilterModel : videoArrManager.getImageObjectFilterRenders()) {
                        if (imageObjectFilterModel.getImagePaths().equals(picArr.get(position))) {
                            return;
                        }
                    }

                    ImageObjectFilterRender imageObjectFilterRender = new ImageObjectFilterRender();
                    imageObjectFilterRender.setImage(FileUtils.loadImageFromLocal(picArr.get(position)));
                    videoArrManager.getOpenGlView().addFilter(imageObjectFilterRender);
                    currentObjectFilterRender = imageObjectFilterRender;
                    currentStickerIndex = position;
                    currentItemCheckImage = checkImg;
                    sticker_control_panel.setVisibility(VISIBLE);
                    videoArrManager.getImageObjectFilterRenders().add(new ImageObjectFilterModel().loadImageObjectFilterModel((ImageObjectFilterRender) currentObjectFilterRender, picArr.get(currentStickerIndex)));
                } else {
                    for (ImageObjectFilterModel imageObjectFilterModel : videoArrManager.getImageObjectFilterRenders()) {
                        if (imageObjectFilterModel.getImagePaths().equals(picArr.get(position))) {
                            videoArrManager.getOpenGlView().removeFilter(imageObjectFilterModel.getImageObjectFilterRender());
                            videoArrManager.getImageObjectFilterRenders().remove(imageObjectFilterModel);
                            return;
                        }
                    }
                }
            }
        });
    }
}
